const userlist = require('../data/user.js');

module.exports = {
    //检查登录
    checkLogin(req, res, next) {
        console.log(req.session.uid);
        if (!req.session.uid) {
            if(req.url=="/login"){
                next();
            }else{
                res.json({
                    success: false,
                    msg: '未登录'
                })
            }
        }else{
            next();
        }
    },
    //登录
    login(req, res, next) {
        let user = userlist.filter(item => item.username == req.body.username)[0];
        if(!user) {
            res.json({
                success: false,
                msg: "用户名不存在"
            });
        }else{
            if(user.password !== req.body.password){
                res.json({
                    success: false,
                    msg: "密码有误"
                });
            }else{
                req.session.uid = user.id;
                console.log(req.session.uid);
                res.json({
                    success: true,
                    msg: "登陆成功",
                    user
                });
            }
        } 
    }
}